//
//  YCPickerViewController.m
//  LOLBox
//
//  Created by mac on 15-4-2.
//  Copyright (c) 2015年 Honey. All rights reserved.
//

#import "YCPickerViewController.h"
#import "UIViewController+CWPopup.h"

@interface YCPickerViewController ()

@property (weak, nonatomic) IBOutlet UIPickerView *picker;
@property (weak, nonatomic) IBOutlet UIButton *canel;
@property (weak, nonatomic) IBOutlet UIButton *save;

@property (weak, nonatomic) IBOutlet UIView *bgView;

@property (nonatomic, strong) NSMutableArray *Serverdata;
@property (nonatomic, strong) NSMutableArray *Fnamedata;

@property (nonatomic, strong) NSString *currentServerName;//记录当前区
@property (nonatomic, strong) NSString *currentFName;

@end

@implementation YCPickerViewController


- (IBAction)dissmi:(UITapGestureRecognizer *)sender
{
    _dissBlock();
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //self.view.alpha = 0.2;
        self.Serverdata = [NSMutableArray array];
        self.Fnamedata = [NSMutableArray array];
        [self getData];
        _currentServerName = self.Serverdata[0];
        _currentFName = self.Fnamedata[0];
        
        self.bgView.frame = CGRectMake(0, CGRectGetHeight(self.view.bounds) - 205, CGRectGetWidth(self.view.bounds), 205);
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.frame = self.view.bounds;
    self.view.backgroundColor = [UIColor blackColor];
    self.view.alpha = 0.9;
    
    UIView *v = [[UIView alloc] initWithFrame:self.view.bounds];
    v.backgroundColor = [UIColor redColor];
    //[self.view addSubview:v];
}
- (IBAction)cancelViewcontroll:(UIButton *)sender
{
    _dissBlock();
}
- (IBAction)saveAnddissVIewcontroler:(UIButton *)sender
{
    //关闭视图前传值过去
    _serverNameBlock(_currentServerName,_currentFName);
    _dissBlock();
}

//关闭视图


- (void)getData
{
    NSString *file = [[NSBundle mainBundle] pathForResource:@"apiServers" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:file];
    
    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
    
    [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
        [self.Serverdata addObject:obj[@"sn"]];
        [self.Fnamedata addObject:obj[@"fn"]];
    }];
}

//有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.Serverdata.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    NSString *s = [NSString stringWithFormat:@"%@ %@",self.Serverdata[row],self.Fnamedata[row]];
    return s;
}
//选中指定内容
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSLog(@"row - %d  , com: %@",row,self.Serverdata[row]);
    _currentServerName = self.Serverdata[row];
    _currentFName = self.Fnamedata[row];
}

- (void)getServerName:(void (^)(NSString *, NSString *))serName
{
    _serverNameBlock = serName;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
